package xmlexample;

/***************************************************************************
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 *
 ***************************************************************************/

/*
 * Utilita.java
 *
 * Created on 24 giugno 2003, 20.14
 */


import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Window;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;


/** utility class with only static methods
 * @author ermanno
 */
public class Utilita {
	private Utilita(){}

  /** centers a container in a dimension
   * @param c our window
   * @param larghezza width of our window
   * @param altezza heigth of our window
   * @param dimSchermo dimension of the screen or the container in which our window is centered
   */
  public static void centraFrame(Container c,int larghezza, int altezza, Dimension dimSchermo) {
    if (c==null||dimSchermo==null)throw new NullPointerException("Valori nulli non permessi.");
    if (larghezza <=0) throw new ArithmeticException("Valori del frame non permessi1");
    if (altezza <=0) throw new ArithmeticException("Valori del frame non permessi2");
    if (larghezza > dimSchermo.width ) throw new ArithmeticException("Valori del frame non permessi3");
    if( altezza > dimSchermo.height) throw new ArithmeticException("Valori del frame non permessi4");
    c.setSize(larghezza,altezza);
    c.setLocation((dimSchermo.width-larghezza)/2,(dimSchermo.height-altezza)/2);
  }

  /** Centers a Window
   * @param screenSize screen dimension
   * @param w a reference to the window to be centered
   */
  public static void center(Window w, Dimension screenSize) {

    Dimension frameSize = w.getSize();

    if (frameSize.height > screenSize.height)
      frameSize.height = screenSize.height;
    if (frameSize.width > screenSize.width)
      frameSize.width = screenSize.width;

    w.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
  }

  public static String save(String fileNamewoutExt,String extension,String content)  {//bisogna dare anche il punto con l'estensione
    try {
      String fileName=fileNamewoutExt+extension;
      FileWriter fw=new FileWriter(fileName);
      BufferedWriter bw=new BufferedWriter(fw);
      bw.write(content);
      bw.close();
      fw.close();
      return "\nFile "+fileName+" salvato.";

    }  catch (IOException ioe) {
      return "\nImpossibile salvare: "+ioe;
    }
  }

  public static String save(File file,String extension,String content) {
    String name=removeExtension(file.getAbsolutePath());
    return save(name,extension,content);
  }

  public static String removeExtension(String name)   {
    int lastIndOfBar =name.lastIndexOf(File.separatorChar);
    int lastIndOfDot=name.lastIndexOf('.');
    return lastIndOfDot>lastIndOfBar?name.substring(0,lastIndOfDot):name;
  }

  public static String readFile(String fileName)  {
    StringBuffer sb=new StringBuffer("\r\n");
  try {
    FileReader fr=new FileReader(fileName);
    BufferedReader br=new BufferedReader(fr);
    String line=br.readLine();
    while (line!=null)  {
      sb.append(line+"\r\n");
      line=br.readLine();
    }
    return sb.toString();

  } catch (IOException e) {
    return ("Impossibile leggere: "+e);
  }

  }

  //routine che aggiunge un componente al GriBagLayout
  //copiata quasi di sana pianta da Java-tecnicheav....

  public static void aggiungiComponente(GridBagLayout gbLayout, GridBagConstraints gbCostr, Container cont, Component c,int rig, int col,int largh, int alt) {
    if (cont.getLayout()!= gbLayout) cont.setLayout(gbLayout);
    gbCostr.gridx=col;
    gbCostr.gridy=rig;

    gbCostr.gridwidth=largh;
    gbCostr.gridheight=alt;

    gbLayout.setConstraints(c,gbCostr);
    cont.add(c);
  }

}
